package co.edu.javeriana.konigsberg.xavier.servicio;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;

import co.edu.javeriana.konigsberg.xavier.dto.TemplateCorreoDTO;

@Singleton
@Startup
public class CorreoServicio implements CorreoServicioLocal, CorreoServiciosRemote{
	public static final int PRODUCCION = 0;
	public static final String EMAIL_SESSION_JNDI_PATH = "java:/XavierMail";  
	
	@Resource(name = "java:/XavierMail")  
    private Session mailSession;  
	
	
//	public static Properties props = new Properties();
//	static {
//	    props.setProperty("mail.smtp.host", "smtp.gmail.com");
//	    props.setProperty("mail.smtp.starttls.enable", "true");
//	    props.setProperty("mail.smtp.port", "587");
//	    props.setProperty("mail.smtp.user", "xavierjaveriana@gmail.com");
//	    props.setProperty("mail.smtp.auth", "true");
//	    
//	}

	private Session getEmailSession() throws Exception{  
        InitialContext context = new InitialContext();  
        return (Session) context.lookup(EMAIL_SESSION_JNDI_PATH);  
    }  
	
	@Override  
    @PermitAll()  
    public void enviarCorreo(TemplateCorreoDTO correoDTO, String[] correos){  
        try{  
            MimeMessage m = new MimeMessage(getEmailSession());
            InternetAddress[] to = null;
            if(PRODUCCION == 1){
            	to = new InternetAddress[correos.length];
            	for (int i = 0; i < correos.length; i++) {
            		InternetAddress internetAddress = new InternetAddress(correos[i]);
            		to[i] = internetAddress;
				}
            }else{
	            to = new InternetAddress[] {  
	            		//new InternetAddress("andersson.perez@javeriana.edu.co"),
	            		//new InternetAddress("fernandopantoja@javeriana.edu.co"),
	            		//new InternetAddress("john.velandia@javeriana.edu.co"),  
	                    new InternetAddress("omellizo@gmail.com")//  
	            };  
        	}
            m.setRecipients(Message.RecipientType.TO, to);  
            m.setSubject(correoDTO.getAsunto());  
            m.setSentDate(new java.util.Date());  
            m.setText(correoDTO.getTexto(), "ISO-8859-1", "html");  
            //  
            Transport.send(m);  
            //  
            //return true;  
        }catch(Exception e){  
            e.printStackTrace();  
            //return false;  
        }  
    }  
	
//	public void enviarCorreo(TemplateCorreoDTO correoDTO){
//		// Preparamos la sesion
//        Session session = Session.getDefaultInstance(props);
//		
//		// Construimos el mensaje
//        MimeMessage message = new MimeMessage(session);
//        try {
//			message.setFrom(new InternetAddress("xavier@xavier.com"));
//			
//			message.addRecipient(Message.RecipientType.TO, new InternetAddress("omellizo@gmail.com"));
//	        message.setSubject(correoDTO.getAsunto());
//	        message.setText(correoDTO.getTexto(),
//	        		"ISO-8859-1",
//	        		"html");
//
//	        // Lo enviamos.
//	        Transport t = session.getTransport("smtp");
//	        t.connect("xavierjaveriana@gmail.com", "xavier2015");
//	        t.sendMessage(message, message.getAllRecipients());
//
//	        // Cierre.
//	        t.close();
//		} catch (AddressException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (MessagingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//        
//		
//	}
	
}
